<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <bread>
        <bread-item to="/">首页</bread-item>
        <bread-item :to="`/category/${goods?.categories[1]?.id}`">{{ goods?.categories[1]?.name }}</bread-item>
        <bread-item :to="`/category/sub/${goods?.categories[0]?.id}`">{{ goods?.categories[0]?.name }}</bread-item>
        <bread-item>{{ goods.name }}</bread-item>
      </bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods" />
          <!-- sku组件 skuId="1369155865461919746" 测试选中 -->
          <GoodsSku :goods="goods" @change="changeSku" />
          <!-- 数量选择组件 -->
          <numbox label="数量" v-model="num" :max="goods.inventory" />
          <!-- 按钮组件 -->
          <mybutton @click="insertCart()" type="primary" size="middle" style="margin-top:20px">加入购物车</mybutton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+周热销榜 -->
        <div class="goods-aside">
          <GoodsHot />
          <GoodsHot :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup >
import { ref, watch, nextTick, provide } from 'vue';
import { useRoute } from 'vue-router';
import Product from "@/api/modules/products"
import GoodsRelevant from "./cpns/goods-relevant.vue"
import GoodsImage from "./cpns/goods-image.vue"
import GoodsSales from "./cpns/goods-sales.vue"
import GoodsName from "./cpns/goods-name.vue"
import GoodsSku from "./cpns/goods-sku.vue"
import GoodsTabs from "./cpns/goods-tabs.vue"
import GoodsHot from "./cpns/goods-hot.vue"
import GoodsWarn from "./cpns/goods-warn.vue"
import Message from "@/base-ui/message/message"
import { useStore } from 'vuex';


let goods = ref(null)
const num = ref(1)
provide("goods", goods)
const route = useRoute()
const store = useStore()
watch(() => route.params.id, (newVal) => {
  if (newVal && `/product/${newVal}` === route.path) {
    Product.goodsList(route.params.id).then(data => {
      // 让商品数据为null然后使用v-if的组件可以重新销毁和创建
      goods.value = null
      nextTick(() => {
        goods.value = data.result
      })
    })
  }
}, { immediate: true })

const currSku = ref(null)
const changeSku = (sku) => {
  if (sku.skuId) {
    goods.value.price = sku.price
    goods.value.oldPrice = sku.oldPrice
    goods.value.inventory = sku.inventory
  }
  // 记录选择后的sku，可能有数据，可能没有数据{}
  currSku.value = sku
}


const insertCart = () => {
  if (currSku.value && currSku.value.skuId) {
    // id skuId name attrsText picture price nowPrice selected stock count isEffective
    const { skuId, specsText: attrsText, inventory: stock } = currSku.value
    const { id, name, price, mainPictures } = goods.value
    console.log(11);
    store.dispatch('cart/insertCart', {
      skuId,
      attrsText,
      stock,
      id,
      name,
      price,
      nowPrice: price,
      picture: mainPictures[0],
      selected: true,
      isEffective: true,
      count: num.value
    }).then(() => {
      Message({ type: 'success', text: '加入购物车成功' })
    })
  } else {
    Message({ text: '请选择完整规格' })
  }
}





</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
// .goods-warn {
//   min-height: 600px;
//   background: #fff;
//   margin-top: 20px;
// }
</style>
